package com.yuwen.yuwenRPC.protocol;

import lombok.Data;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 协议 序列化器枚举类
 */
public enum ProtocolMessageSerializerEnum {
    JDK(0,"jdk"),
    JSON(1,"json"),
    KRYO(2,"kryo"),
    HESSIAN(3,"hessian"),
    ;


    private final int key;
    private final String value;

    ProtocolMessageSerializerEnum(int key, String value) {
        this.key = key;
        this.value = value;
    }

    /**
     * 获取值列表
     * @return List
     */
    public static List<String> getValues(){
        return Arrays.stream(values()).map(item->{
            return item.value;
        }).collect(Collectors.toList());

    }

    /**
     * 根据key 获取序列化器
     * @param key
     * @return
     */
     public static ProtocolMessageSerializerEnum getByKey(int key) {
        for (ProtocolMessageSerializerEnum protocolMessageSerializerEnum : ProtocolMessageSerializerEnum.values()) {
            if (protocolMessageSerializerEnum.key == key) {
                return protocolMessageSerializerEnum;
            }
        }
        return null;
     }


    /**
     * 根据value  获取 序列化器
     * @param value
     * @return
     */
    public static ProtocolMessageSerializerEnum getSerializerByValue(String value) {
        for (ProtocolMessageSerializerEnum serializer : ProtocolMessageSerializerEnum.values()) {
            if (serializer.value.equals(value)) {
                return serializer;
            }
        }
        return null;
    }

    public int getKey() {
        return key;
    }

    public String getValue() {
        return value;
    }
}